package com.wef.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;

import com.gfa4j.exception.ValidateRuntimeException;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.system.entity.MenuGroup;
import com.wef.system.entity.Module;
import com.wef.system.entity.domain.MenuGroupDomain;
import com.wef.system.entity.domain.ModuleDomain;
import com.wef.system.service.MenuGroupService;
import com.wef.system.service.MenuGroupTitleService;

public class MenuGroupServiceImpl extends BaseServiceImpl implements MenuGroupService {

	private MenuGroupTitleService menuGroupTitleService;

	public void setMenuGroupTitleService(MenuGroupTitleService menuGroupTitleService) {
		this.menuGroupTitleService = menuGroupTitleService;
	}

	public MenuGroup addMenuGroup(MenuGroup menuGroup) {
		validateMenuGroup(menuGroup);
		MenuGroup newMenuGroup = (MenuGroup) super.saveObject(menuGroup);
		if (Hibernate.isPropertyInitialized(menuGroup, MenuGroupDomain.menuGroupTitles) && menuGroup.getMenuGroupTitles().size() > 0) {
			menuGroupTitleService.addMenuGroupTitles(menuGroup.getMenuGroupTitles(), newMenuGroup.getMenuGroupId());
		}
		return newMenuGroup;
	}

	public MenuGroup addMenuGroupByParentId(MenuGroup menuGroup, String parentId) {
		validateMenuGroup(menuGroup);
		List moduleList = super.findByProperty(Module.class, ModuleDomain.moduleId, parentId);

		Module module = (Module) moduleList.get(0);
		menuGroup.setModule(module);

		MenuGroup newMenuGroup = (MenuGroup) super.saveObject(menuGroup);

		if (Hibernate.isPropertyInitialized(menuGroup, MenuGroupDomain.menuGroupTitles) && menuGroup.getMenuGroupTitles().size() > 0) {
			menuGroupTitleService.addMenuGroupTitles(menuGroup.getMenuGroupTitles(), newMenuGroup.getMenuGroupId());
		}
		return newMenuGroup;
	}

	public MenuGroup updateMenuGroup(MenuGroup menuGroup) {
		if (Hibernate.isPropertyInitialized(menuGroup, MenuGroupDomain.menuGroupTitles) && menuGroup.getMenuGroupTitles().size() > 0) {
			menuGroupTitleService.addMenuGroupTitles(menuGroup.getMenuGroupTitles(), menuGroup.getMenuGroupId());
		}
		return (MenuGroup) super.updateObject(menuGroup);
	}

	public MenuGroup updateMenuGroupById(MenuGroup menuGroup) {
		if (Hibernate.isPropertyInitialized(menuGroup, MenuGroupDomain.menuGroupTitles) && menuGroup.getMenuGroupTitles().size() > 0) {
			menuGroupTitleService.addMenuGroupTitles(menuGroup.getMenuGroupTitles(), menuGroup.getMenuGroupId());
		}

		List menuGroupList = super.findByProperty(MenuGroup.class, MenuGroupDomain.menuGroupId, menuGroup.getMenuGroupId());

		MenuGroup updateMenuGroup = (MenuGroup) menuGroupList.get(0);
		updateMenuGroup.setMenuGrpSeq(menuGroup.getMenuGrpSeq());
		updateMenuGroup.setMenuGrpDescKey(menuGroup.getMenuGrpDescKey());

		return updateMenuGroup;
	}

	public void removeMenuGroup(MenuGroup menuGroup) {

		List<String> initPropertys = new ArrayList<String>();
		initPropertys.add(MenuGroupDomain.menuGroupTitles);
		menuGroup = (MenuGroup) load(MenuGroup.class, menuGroup.getMenuGroupId(), initPropertys);

		if (Hibernate.isPropertyInitialized(menuGroup, MenuGroupDomain.menuGroupTitles) && menuGroup.getMenuGroupTitles().size() > 0) {
			menuGroupTitleService.removeMenuGroupTitles(menuGroup.getMenuGroupTitles());
		}

		this.validateDelete(menuGroup);
		super.deleteObject(menuGroup);
	}

	public MenuGroup saveOrUpdateMenuGroup(MenuGroup menuGroup) {
		if (menuGroup.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addMenuGroup(menuGroup);
		}
		if (menuGroup.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateMenuGroup(menuGroup);
		}
		if (menuGroup.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.removeMenuGroup(menuGroup);
			return MenuGroup.NULL;
		}
		return menuGroup;
	}

	public void saveOrUpdateMenuGroups(List<MenuGroup> menuGroups) {
		StringBuilder errorString = new StringBuilder();
		for (MenuGroup menuGroup : menuGroups) {
			try {
				saveOrUpdateMenuGroup(menuGroup);
			} catch (ValidateRuntimeException ex) {
				errorString.append(ex.getMessage() + "\n");
			}
		}
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
	}

	public void saveOrUpdateMenuGroups(Set<MenuGroup> menuGroups) {
		StringBuilder errorString = new StringBuilder();
		for (MenuGroup menuGroup : menuGroups) {
			try {
				saveOrUpdateMenuGroup(menuGroup);
			} catch (ValidateRuntimeException ex) {
				errorString.append(ex.getMessage() + "\n");
			}
		}
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
	}

	public void validateMenuGroup(MenuGroup menuGroup) {

	}

	public List<MenuGroup> getMenuGroupsByModuleId(String moduleId) {
		Module module = (Module) super.load(Module.class, moduleId);
		List<MenuGroup> menuGroupList = super.findByProperty(MenuGroup.class, MenuGroupDomain.module, module);
		for (MenuGroup menuGroup : menuGroupList) {
			Hibernate.initialize(menuGroup.getMenuGroupTitles());
		}
		return menuGroupList;
	}
}
